package reduce.fz.com.reduce.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.style.ReplacementSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import reduce.fz.com.reduce.R;
import reduce.fz.com.reduce.bean.ReplyComment;
import reduce.fz.com.reduce.bean.User;
import reduce.fz.com.reduce.bean.result.UserWitness;
import reduce.fz.com.reduce.bean.style.TextSpan;
import reduce.fz.com.reduce.custom.FollowView;
import reduce.fz.com.reduce.custom.HeaderFooterStatusRecyclerViewAdapter;
import reduce.fz.com.reduce.init.RecycleOper;
import reduce.fz.com.reduce.util.TextSpanUtil;

/**
 * Created by qingshengzheng on 15/9/16.
 */
public class WitnessDetailAdapter extends HeaderFooterStatusRecyclerViewAdapter<BaseRecycleViewHolder> {
    private UserWitness userWitness=new UserWitness();
    private List<ReplyComment> replys=new ArrayList<ReplyComment>();
    private Context context;
    private LayoutInflater layoutInflater;

    public WitnessDetailAdapter(Context context) {
        this.context = context;
        this.layoutInflater = LayoutInflater.from(context);
    }

    public void addReplays(List<ReplyComment> datas)
    {
        int start=this.replys.size();
        int limit=datas.size();
        this.replys.addAll(datas);
        notifyItemRangeInserted(start,limit);
    }
    public void initReplays(List<ReplyComment> datas)
    {
        this.replys.addAll(datas);
        notifyDataSetChanged();
    }
    public void clearReplays()
    {
        replys.clear();
        notifyDataSetChanged();
    }

    @Override
    public BaseRecycleViewHolder<UserWitness> createFooterStatusViewHolder(View footerView) {
        return new BaseRecycleViewHolder<UserWitness>(footerView) {
            @Override
            public void renderView(UserWitness data,int position) {

            }
        };
    }

    @Override
    protected int getHeaderItemCount() {
        return 1;
    }

    @Override
    protected int getContentItemCount() {
        return replys.size();
    }

    @Override
    protected BaseRecycleViewHolder<UserWitness> onCreateHeaderItemViewHolder(ViewGroup parent, int headerViewType) {
        View view = layoutInflater.inflate(R.layout.witness_card_dynamic_item, parent, false);
        return new WitnessDynamicViewHolder(view);

    }

    @Override
    protected BaseRecycleViewHolder<UserWitness> onCreateContentItemViewHolder(ViewGroup parent, int contentViewType) {
        View view=layoutInflater.inflate(R.layout.layout_cardreply_item,parent,false);
        return new ReplyViewHolder(view);

    }

    @Override
    protected void onBindHeaderItemViewHolder(BaseRecycleViewHolder headerViewHolder, int position) {
        WitnessDynamicViewHolder dynamicViewHolder=(WitnessDynamicViewHolder)headerViewHolder;
        dynamicViewHolder.renderView(userWitness,position);
    }

    @Override
    protected void onBindContentItemViewHolder(BaseRecycleViewHolder contentViewHolder, int position) {
       ReplyViewHolder replyViewHolder= (ReplyViewHolder) contentViewHolder;
        replyViewHolder.renderView(replys.get(position),position);

    }


    public static class ReplyViewHolder extends BaseRecycleViewHolder {
        private ImageView  mUserHeader;
        private  TextView mTvUserNickName;
        private  TextView mTvReplyContent;
        private  TextView mTvReplyDate;

        public ReplyViewHolder(View itemView) {
            super(itemView);
            initWidget(itemView);
        }

        public void initWidget(View itemView) {
            mUserHeader = (ImageView) itemView.findViewById(R.id.user_header);
            mTvUserNickName=(TextView)itemView.findViewById(R.id.tv_user_nickname);
            mTvReplyContent=(TextView)itemView.findViewById(R.id.tv_reply_content);
            mTvReplyDate=(TextView)itemView.findViewById(R.id.tv_reply_date);
        }

        @Override
        public void renderView(Object data,int position) {
            Picasso.with(context).load(R.drawable.shuaige).into(mUserHeader);
//            ReplyComment comment=(ReplyComment) data;
//            Picasso.with(context).load(comment.getUser().getHeadurl());
//            mTvUserNickName.setText(comment.getUser().getNickname());
//            mTvReplyContent.setText(comment.getDesc());
//            mTvReplyDate.setText(comment.getReplydate()+"");

        }
    }


    public static class WitnessDynamicViewHolder extends BaseRecycleViewHolder<UserWitness> implements View.OnClickListener {
        private ImageView user_header;
        private TextView tv_user_nickname;
        private TextView pub_time;
        private FollowView user_follow;
        private TextView card_desc;
        private ImageView card_main;
        private RecyclerView witnessPics;
        private RecyclerView witnessUsers;
        private RelativeLayout rl_witness;
        private RelativeLayout rl_comment;
        private RelativeLayout rl_share;
        private LinearLayout ll_comment_list;
        private WitnessPicHorAdapter  picAdapter=null;
        private HeaderHorAdapter witHeaderAdapter=null;
        private  PubCardDynamicListener pubCardDynamicListener;


        public WitnessDynamicViewHolder(View itemView) {
            super(itemView);
            initWidget(itemView);
        }

        public WitnessDynamicViewHolder(View itemView,PubCardDynamicListener pubCardDynamicListener)
        {
            super(itemView);
            this.pubCardDynamicListener=pubCardDynamicListener;
        }

        @Override
        public void renderView(UserWitness wit,int position) {
           /* Picasso.with(context).load(wit.user.getHeadurl()).into(user_header);
            tv_user_nickname.setText(wit.user.nickname);
            pub_time.setText(wit.pubTime.toString());
            user_follow.setFollowUser(wit.user.getUid());
            card_desc.setText(wit.card.getDesc());
            Picasso.with(context).load(wit.card.mainImage).into(card_main);
            witnessPics.setAdapter(new WitnessPicHorAdapter(context));
            witnessUsers.setAdapter(new HeaderHorAdapter(context));*/

            Picasso.with(context).load(R.drawable.shuaige).into(user_header);
            //tv_user_nickname.setText(wit.user.nickname);
            //pub_time.setText(wit.pubTime.toString());
            //user_follow.setFollowUser(wit.user.getUid());
            // card_desc.setText(wit.card.getDesc());
            Picasso.with(context).load(R.drawable.paobu_witness_card).into(card_main);
            picAdapter=new WitnessPicHorAdapter(context);
            witHeaderAdapter=new HeaderHorAdapter(context);
            RecycleOper oper1=new RecycleOper(context,witnessPics);
            RecycleOper oper2=new RecycleOper(context,witnessUsers);
            oper1.setHorDefault();
            oper2.setHorDefault();
            witnessPics.setAdapter(picAdapter);
            witnessUsers.setAdapter(witHeaderAdapter);
            List<String> temp=new ArrayList<String>();
            for(int i=0;i<6;i++)
            {
                temp.add(new String(""));
            }
            picAdapter.initDatas(temp);
            List<User> tempusers=new ArrayList<User>();
            for(int i=0;i<10;i++)
            {
                tempusers.add(new User());
            }
            witHeaderAdapter.initDatas(tempusers);

        }

        public void initCommentList(UserWitness wit)
        {
            String reply="{0}说:{1}";
            List<ReplyComment> comments=wit.card.comments;
            for(int i=0;i<comments.size();i++)
            {
                ReplyComment comment=comments.get(i);
                TextView view=new TextView(context);
                TextSpan span=new TextSpan(comment.user.nickname,R.style.reply_comment_name_text_style);
                TextSpan span1=new TextSpan(comment.desc,R.style.reply_comment_defalut_style);
                TextSpanUtil.set(context, view, null, R.style.reply_comment_defalut_style, span, span1);
                ll_comment_list.addView(view);
            }
        }
        public void initWidget(View view) {
            user_header = (ImageView) view.findViewById(R.id.user_header);
            tv_user_nickname = (TextView) view.findViewById(R.id.tv_user_nickname);
            pub_time = (TextView) view.findViewById(R.id.pub_time);
            user_follow = (FollowView) view.findViewById(R.id.user_follow);
            card_desc = (TextView) view.findViewById(R.id.card_desc);
            card_main = (ImageView) view.findViewById(R.id.card_main);
            witnessPics = (RecyclerView) view.findViewById(R.id.witness_pics);
            witnessUsers = (RecyclerView) view.findViewById(R.id.witness_user_list);
            rl_witness = (RelativeLayout) view.findViewById(R.id.rl_witness);
            rl_comment = (RelativeLayout) view.findViewById(R.id.rl_comment);
            rl_share = (RelativeLayout) view.findViewById(R.id.rl_share);
            ll_comment_list=(LinearLayout)view.findViewById(R.id.witness_comments_list);
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.rl_witness:
                    pubCardDynamicListener.witness();
                    break;
                case R.id.rl_share:
                    pubCardDynamicListener.share();
                    break;
                case R.id.rl_comment:
                    pubCardDynamicListener.comment();
                    break;
                default:
                    break;
            }
        }
    }

    public interface PubCardDynamicListener{
        public void witness();
        public void share();
        public void comment();
    }
}